$(document).ready(function() {
	setTimeout(function() {
		$('div#msg').fadeOut(2000);
	}, 2000);
	$("#errorMessage").hide();
	$('.ui-widget-overlay').live('click', function() { $("#loginDialog").dialog("close"); });
	if($('#imageViewer').length > 0) $('#imageViewer a').lightBox();
	
	if (screen.width >= 1360) {
		$('#floatdiv1').addFloating(  
				{  
					targetRight: 40,  
					targetTop: 10,  
					snap: false  
		});
		
		$('#floatdiv2').addFloating(  
            {  
                targetLeft: 40,  
                targetTop: 10,  
                snap: false  
    });  
	} else {
		$('#floatdiv1').css("display", "none");
		$('#floatdiv2').css("display", "none");
	}	
	
	
});

$.urlParam = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results)
    { 
        return 0; 
    }
    return results[1] || 0;
};

function closeLogInDialog(){
	$("#loginDialog").dialog("close");
}

function login() {
	$.post("../action/userAction.php", 
			{ username : $("#username").val(), password : $("#password").val() },
			function(response) {
				if (response == 'y') {
					$("#loginDialog").dialog("close");
					// Back to home page
					window.location.href = "../pc/index.php";
//					var currentUrl = location + "";
//					if(currentUrl.indexOf("?") != -1)
//						window.location.href = currentUrl.substring(currentUrl.lastIndexOf("/") + 1, currentUrl.indexOf("?"));
//					else 
//						window.location.href = currentUrl;
				} else {
					$("#errorMessage").show();
				}
			}
	);
}

function showLogin() {
	$("#loginDialog").dialog({ 
		dialogClass: 'no-close',
		modal: true,
		position: 'center',
		width: '603px',
		close: function(event, ui) {
			$("#msg").val("");
			$("#username").val("");
			$("#password").val("");
			$("#errorMessage").hide();
		}
	});
}

function showAddedToCartDialog() {
	$("#addedToCartDialog").dialog({ 
		dialogClass: 'no-close',
		modal: true,
		position: 'center'
	}).dialog("widget")
	.next(".ui-widget-overlay")
	.css("background", "#f00ba2")
	.css("opacity","0.1");
	setTimeout(function() {
		$("#addedToCartDialog").dialog("close");
	},2000);
}

function resetForm(form){
	document.getElementById('' + form).reset();
}

function submitForm(formId, url, method) {
	$("#" + formId).attr("method", method);
	$("#" + formId).attr("action", url);
	$("#" + formId).submit();
}

function hideMessage() {
    var ok = document.getElementById("msg");
    addClass(ok, 'msg-hidden');
}

function addClass(el, name){
    el.className += (el.className ? ' ' : '') +name;
}

function addToCart(productId, quantity) {
    $.post("../action/AddProductToCartAction.php",
           { pro_id: productId, qty: quantity },
           function(response) {
               if (response == "needLogin") {
            	   showLogin();
               } else {
            	   showAddedToCartDialog();
               }
           }
	);
}

// ///////////////////////////////////
// Cookie handle
//
function getCookie(c_name) {
	var i, x, y, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++) {
		x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g, "");
		if (x == c_name) {
			return unescape(y);
		}
	}
}

function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value)
			+ ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
	document.cookie = c_name + "=" + c_value;
}

// //////////////////////////////
// call loadP and unloadP when body loads/unloads and scroll position will not
// move
function getScrollXY() {
	var x = 0, y = 0;
	if (typeof (window.pageYOffset) == 'number') {
		// Netscape
		x = window.pageXOffset;
		y = window.pageYOffset;
	} else if (document.body
			&& (document.body.scrollLeft || document.body.scrollTop)) {
		// DOM
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	} else if (document.documentElement
			&& (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		// IE6 standards compliant mode
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	return [ x, y ];
}

function setScrollXY(x, y) {
	window.scrollTo(x, y);
}

function loadP(pageref) {
	x = getCookie(pageref + 'x');
	y = getCookie(pageref + 'y');
	setScrollXY(x, y);
}

function unloadP(pageref) {
	s = getScrollXY();
	setCookie(pageref + 'x', s[0], 1);
	setCookie(pageref + 'y', s[1], 1);
}
